﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Server.MessageModule.Message
{
    class CommandMessage : BaseNetworkMessage
    {
        private string _command;
        private Dictionary<string, object> _params;
        public CommandMessage()
            : base(ShareMacro.MESSAGETYPE_COMMAND)
        {
            _params = new Dictionary<string, object>();
        }

        public string Command { get { return _command; } set { _command = value; } }

        public void addParam(string key, object data)
        {
            if (data == null)
                return;

            if (_params.ContainsKey(key))
                _params.Remove(key);
            _params.Add(key, data);
        }

        public object getParam(String key)
        {
            if (key == null)
                return null;
            if (_params.ContainsKey(key))
                return _params[key];
            else
                return null;
        }

        public override string Serialize()
        {
            return null;
        }

        public override void Deserialize(string data)
        {
            Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(data);
            if (dic == null)
                throw new Exception("Null pointer exeption!");
            _command = dic[ShareMacro.TAG_COMMAND] as string;
            _params = null;
            _params = JsonConvert.DeserializeObject<Dictionary<string, object>>(dic[ShareMacro.TAG_PARAMS].ToString());
        }
    }
}
